<!doctype html>
<html lang="en">
<head>
    <title>Code coverage report for components\lazy\lazy.js</title>
    <meta charset="utf-8" />
    <link rel="stylesheet" href="../../prettify.css" />
    <link rel="stylesheet" href="../../base.css" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style type='text/css'>
        .coverage-summary .sorter {
            background-image: url(../../sort-arrow-sprite.png);
        }
    </style>
</head>
<body>
<div class='wrapper'>
  <div class='pad1'>
    <h1>
      <a href="../../index.html">all files</a> / <a href="index.html">components/lazy/</a> lazy.js
    </h1>
    <div class='clearfix'>
      <div class='fl pad1y space-right2'>
        <span class="strong">3.51% </span>
        <span class="quiet">Statements</span>
        <span class='fraction'>2/57</span>
      </div>
      <div class='fl pad1y space-right2'>
        <span class="strong">0% </span>
        <span class="quiet">Branches</span>
        <span class='fraction'>0/33</span>
      </div>
      <div class='fl pad1y space-right2'>
        <span class="strong">0% </span>
        <span class="quiet">Functions</span>
        <span class='fraction'>0/16</span>
      </div>
      <div class='fl pad1y space-right2'>
        <span class="strong">4% </span>
        <span class="quiet">Lines</span>
        <span class='fraction'>2/50</span>
      </div>
    </div>
  </div>
  <div class='status-line low'></div>
<pre><table class="coverage">
<tr><td class="line-count quiet">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104</td><td class="line-coverage quiet"><span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">1×</span>
<span class="cline-any cline-yes">1×</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span></td><td class="text"><pre class="prettyprint lang-js">import { throttle, _ } from '@/utils/utils.js'
import Listener from './listener.js'
const DEFAULT_EVENTS = ['scroll', 'wheel', 'mousewheel', 'resize', 'animationend', 'transitionend', 'touchmove']
const LOADING_IMAGE = ''
export default (Vue, options) =&gt; <span class="fstat-no" title="function not covered" >{</span>
<span class="cstat-no" title="statement not covered" >	return new class {</span>
		constructor({loading, error, delay, listenEvents}) <span class="fstat-no" title="function not covered" >{</span>
<span class="cstat-no" title="statement not covered" >			this.loading = loading || LOADING_IMAGE</span>
<span class="cstat-no" title="statement not covered" >			this.listenEvents = listenEvents || DEFAULT_EVENTS</span>
<span class="cstat-no" title="statement not covered" >			this.error = error || ''</span>
			// this.targetQueue = [];
<span class="cstat-no" title="statement not covered" >			this.listenerQueue = []</span>
<span class="cstat-no" title="statement not covered" >			this.delay = delay || 320</span>
			// 节流函数
<span class="cstat-no" title="statement not covered" >			this.lazyLoadHandler = throttle(this._lazyLoadHandler.bind(this), this.delay)</span>
<span class="cstat-no" title="statement not covered" >			this._initEvent()</span>
		}
		// 指令初始化时添加相应的监听器
		add(el, binding, vnode) <span class="fstat-no" title="function not covered" >{</span>
<span class="cstat-no" title="statement not covered" >			if (this.listenerQueue.some(item =&gt; <span class="cstat-no" title="statement not covered" ><span class="fstat-no" title="function not covered" >item.el === el)</span></span>) {</span>
<span class="cstat-no" title="statement not covered" >					this.update(el, binding)</span>
<span class="cstat-no" title="statement not covered" >					return Vue.nextTick(this.lazyLoadHandler)</span>
			}
			// 得到图片绑定的src
			let src = <span class="cstat-no" title="statement not covered" >binding.value</span>
<span class="cstat-no" title="statement not covered" >				Vue.nextTick(() =&gt; <span class="fstat-no" title="function not covered" >{</span></span>
					const listener = <span class="cstat-no" title="statement not covered" >new Listener({</span>
						el,
						loading: this.loading,
						error: this.error,
						src,
						elRender: this._elRender.bind(this),
						bindType: binding.arg
				})
				// 在相应的元素上注册滚动添加元素，目前仅支持document的scroll事件
				// this._addTargetEle(document);
<span class="cstat-no" title="statement not covered" >				this.listenerQueue.push(listener)</span>
				// 尽快加载图片
<span class="cstat-no" title="statement not covered" >				this.lazyLoadHandler()</span>
<span class="cstat-no" title="statement not covered" >				Vue.nextTick(() =&gt; <span class="fstat-no" title="function not covered" >{ <span class="cstat-no" title="statement not covered" >this.lazyLoadHandler() </span>})</span></span>
			})
		}
		// 当组件更新时候，更新状态
		update(el, binding) <span class="fstat-no" title="function not covered" >{</span>
			const src = <span class="cstat-no" title="statement not covered" >binding.value</span>
			const listener = <span class="cstat-no" title="statement not covered" >this.listenerQueue.find(listener =&gt; <span class="cstat-no" title="statement not covered" ><span class="fstat-no" title="function not covered" >listener.el === el)</span></span></span>
<span class="cstat-no" title="statement not covered" >			listener &amp;&amp; listener.update(src)</span>
<span class="cstat-no" title="statement not covered" >			this.lazyLoadHandler()</span>
<span class="cstat-no" title="statement not covered" >			Vue.nextTick(() =&gt; <span class="cstat-no" title="statement not covered" ><span class="fstat-no" title="function not covered" >this.lazyLoadHandler())</span></span></span>
		}
		// 卸载元素时，根据需要移除元素上listener
		remove(el, binding) <span class="fstat-no" title="function not covered" >{</span>
<span class="cstat-no" title="statement not covered" >			if (!el) <span class="cstat-no" title="statement not covered" >return</span></span>
			const listener = <span class="cstat-no" title="statement not covered" >this.listenerQueue.find(listener =&gt; <span class="cstat-no" title="statement not covered" ><span class="fstat-no" title="function not covered" >listener.el === el)</span></span></span>
<span class="cstat-no" title="statement not covered" >			if (listener) {</span>
				let index = <span class="cstat-no" title="statement not covered" >this.listenerQueue.indexOf(listener)</span>
<span class="cstat-no" title="statement not covered" >				this.listenerQueue.splice(index, 1) &amp;&amp; listener.destroy()</span>
			}
		}
		// 事件的渲染函数，目前只支持img元素，后续可以添加background-url情况
		_elRender(listener, state) <span class="fstat-no" title="function not covered" >{</span>
<span class="cstat-no" title="statement not covered" >			if (!listener.el) <span class="cstat-no" title="statement not covered" >return</span></span>
			const { el, bindType } = <span class="cstat-no" title="statement not covered" >listener</span>
			let src
<span class="cstat-no" title="statement not covered" >			switch (state) {</span>
				case 'loading':
<span class="cstat-no" title="statement not covered" >				src = listener.loading</span>
<span class="cstat-no" title="statement not covered" >				break</span>
				case 'error':
<span class="cstat-no" title="statement not covered" >				src = listener.error</span>
<span class="cstat-no" title="statement not covered" >				break</span>
				default:
<span class="cstat-no" title="statement not covered" >				src = listener.src</span>
<span class="cstat-no" title="statement not covered" >				break</span>
			}
<span class="cstat-no" title="statement not covered" >			if (bindType) {</span>
<span class="cstat-no" title="statement not covered" >				el.style[bindType] = `url("${src}")`</span>
			} else <span class="cstat-no" title="statement not covered" >if (el.getAttribute('src') !== src) {</span>
<span class="cstat-no" title="statement not covered" >				el.setAttribute('src', src)</span>
			}
<span class="cstat-no" title="statement not covered" >			el.setAttribute('lazy', state)</span>
		}
		// 初始化触发事件
		_initEvent() <span class="fstat-no" title="function not covered" >{</span>
<span class="cstat-no" title="statement not covered" >			this.listenEvents.forEach(eventName =&gt; <span class="fstat-no" title="function not covered" >{</span></span>
				// 初始化时候注册一个函数
<span class="cstat-no" title="statement not covered" >				_.on(document, eventName, this.lazyLoadHandler)</span>
			})
		}
		// 异步处理组件
		_lazyLoadHandler() <span class="fstat-no" title="function not covered" >{</span>
			let visible
<span class="cstat-no" title="statement not covered" >			this.listenerQueue.forEach(listener =&gt; <span class="fstat-no" title="function not covered" >{</span></span>
<span class="cstat-no" title="statement not covered" >				if (listener.state.loading || listener.state.loaded) {</span>
<span class="cstat-no" title="statement not covered" >					return</span>
				}
				// 判断元素是否可见
<span class="cstat-no" title="statement not covered" >				visible = listener.checkInView()</span>
<span class="cstat-no" title="statement not covered" >				visible &amp;&amp; listener.load()</span>
			})
		}
	}(options)
}
&nbsp;</pre></td></tr>
</table></pre>
<div class='push'></div><!-- for sticky footer -->
</div><!-- /wrapper -->
<div class='footer quiet pad2 space-top1 center small'>
  Code coverage
  generated by <a href="http://istanbul-js.org/" target="_blank">istanbul</a> at Sun Feb 24 2019 15:08:50 GMT+0800 (中国标准时间)
</div>
</div>
<script src="../../prettify.js"></script>
<script>
window.onload = function () {
        if (typeof prettyPrint === 'function') {
            prettyPrint();
        }
};
</script>
<script src="../../sorter.js"></script>
</body>
</html>
